$(function(){
	//地图部分
	var map = new BMap.Map("allmap",{enableDblclickZoom:false,enableMapClick:false,minZoom:5,maxZoom:14});  
	map.setMapStyle({
		styleJson: [{
			featureType: 'label',
			elementType: 'all',
			stylers: {
				visibility: 'off'
			}
		}]
	});
	var $map=$('#allmap');
	var present=true;//标记状态 true=全国， false=区间 
	var select_type=0;//1=单量，2=门店，3=服务商，4=工程师 （全球级别时启用）
	var imp={
			min:0,
			max:500,
	};
	map.centerAndZoom(new BMap.Point(107.720476,38.019292), 5);//5~10  10~14
	map.enableScrollWheelZoom();   //开启滚轮放大
	//map.disableScrollWheelZoom();  //关闭滚轮放大
	map.enableContinuousZoom();   //
	map.disableDoubleClickZoom()  //关闭双击放大
	//map.disableDragging();    //禁止拖拽
	//map.enableDragging()      //运行拖拽
	groundOverlayOptions = {
	    opacity: 1,
	    displayOnMinLevel: 5,
	    displayOnMaxLevel: 14
    }
	var mapType1 = new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP]});
	var mapType2 = new BMap.MapTypeControl({anchor: BMAP_ANCHOR_TOP_LEFT});
	var overView = new BMap.OverviewMapControl();
	var overViewOpen = new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_LEFT});
	function add_control(){
		map.addControl(mapType1);          //2D图，卫星图
		/*map.addControl(mapType2);  */        //左上角，默认地图控件
		/*map.setCurrentCity("北京");  */      //由于有3D图，需要设置城市哦
		map.addControl(overView);          //添加默认缩略地图控件
		map.addControl(overViewOpen);      //右下角，打开
		$map.find("div.BMap_omCtrl").css({
			left:"500px"
		});
	}
	add_control();
    var g1 = new gradientColor('#00FF00','#FFFF00',67);
	    g2 = new gradientColor('#FFFF00','#FFA500',67);
	    g3 = new gradientColor('#FFA500','#FF0000',68);
	    g1=g1.concat(g2);
	    g1=g1.concat(g3);  
	      
	var path=window.document.location.href.substring(0,window.document.location.href.indexOf(window.document.location.pathname));
	var projectName = window.document.location.pathname.substring(0,window.document.location.pathname.substr(1).indexOf('/')+2);
	var url = path+projectName;
	//var url = "http://localhost:9000/lmrp/";
	var itsmcode ="xihe";
	var urls={
			item:url+itsmcode+"/map/projects.json",//查询项目
			area:url+itsmcode+"/map/citys.json",//查询地区
			sum_work:url+itsmcode+"/map/mapdata.json",//全国查询工单  ?statisticstype=0&project="+project+"&date="+date
			sum_engineer:url+itsmcode+"/map/mapdata.json",//全国查询 工程师  ?statisticstype=3&project="+project+"&date="+date
			store:url+itsmcode+"/map/regiondata.json",//区间查询上门地址   ?project="+project+"&date="+date
		};
	var datas={
		    sum_engineer:null,
		    sum_service:service,
		    sum_store:store,
		    sum_work:null
		  };
     var qj={
    		  type:1,//查询类型  1=城市查询 point不可为空， 2=区间查询 , 
	          point:{lng:' 116.404',lat:'39.915'},//城市坐标中心点
    		  service: [
	    		               {point:{lng:116.473000,lat:39.916205},city:'斐济',storeName:'华润',mark:'H',address:'北京市xxxxxxxx',count:123,rise:5},
	    		               {point:{lng:115.971008,lat:39.013605},city:'斐济',storeName:'华润',mark:'H',address:'北京市xxxxxxxx',count:423,rise:-5},
	    		               {point:{lng:117.573008,lat:39.416605},city:'斐济',storeName:'华润',mark:'H',address:'北京市xxxxxxxx',count:3,rise:5}
    		           ],
    		  store: null
    		};
     var monthss=[{text:'01 月',value:'01'},
                  {text:'02 月',value:'02'},
                  {text:'03 月',value:'03'},
                  {text:'04 月',value:'04'},
                  {text:'05 月',value:'05'},
                  {text:'06 月',value:'06'},
                  {text:'07 月',value:'07'},
                  {text:'08 月',value:'08'},
                  {text:'09 月',value:'09'},
                  {text:'10 月',value:'10'},
                  {text:'11 月',value:'11'},
                  {text:'12 月',value:'12'}
                 ];
     var ps=  [
               {text:'单量正序',value:1},
               {text:'单量倒序',value:2},
              ];
      //全国类型切换
     function initialize(data,min,max){
    	 md(5);
    	switch (select_type) {
		case 1://单量
			if(data.sum_work){
				data=data.sum_work.list;
			}else{
				data=[];
			}
			break;
		case 2://门店
			if(data.sum_store){
				data=data.sum_store.list;	
			}else{
				data=[];
			}
			break;
		case 3://服务商
			if(data.sum_service){
				data=data.sum_service.list;				
			}else{
				data=[];
			}
			break;
		case 4://工程师
			if(data.sum_engineer){
				data=data.sum_engineer.list;				
			}else{
				data=[];
			}
			break;
		}
    	 map.clearOverlays();
	     for(var i=0;i<data.length;i++){
	    	 if(data[i].count>=min&&data[i].count<=max){
	    		 overlay_round(data[i].point,data[i].city,data[i].count);
	    	 }
	     }
     }
     //插件拖拽
	 $("#th").drag({
		  min:imp.min,
		  max:imp.max,
		  color:g1,
		  onchange:function(opts){
			initialize(datas,opts.the_min,opts.the_max);
		  }
	  });
	$("#drag").css('display','block');
    //插件划图
	$map.paint({
		map:map,
		path:url+'plugins/paint/img',
		onchange:function(opts){
			var mou=opts.mous;
			if(mou.direction==2){
				var lng=mou.begin_lng;
			    var lat=mou.begin_lat;
			    mou.begin_lng=mou.over_lng;
			    mou.begin_lat=mou.over_lat;
			    mou.over_lng=lng;
			    mou.over_lat=lat;
			}else  if(mou.direction==3){
			    var lat=mou.begin_lat;
			    mou.begin_lat=mou.over_lat;
			    mou.over_lat=lat;
			}else if(mou.direction==4){
				var lng=mou.begin_lng;
			    mou.begin_lng=mou.over_lng;
			    mou.over_lng=lng;
			} 
			button.Area52(mou);
			//事件解绑
			$("#allmap").unbind("mousewheel DOMMouseScroll");
			$("body").get(0).addEventListener('mousedown',function(e){
				paint_cancel(e,arguments.callee);
			});
		},
		oncancel:function(opts){
			$("#hf_centre").empty();
			allmap_on();
		}
	});
	function  paint_cancel(e,fun){
		if(e.which==3){
			$map.paint("cancel");
			$("body").get(0).removeEventListener("mousedown",fun);
		}
	};
	function allmap_on(){
		$("#allmap").on("mousewheel DOMMouseScroll",function(e){//地图级别监听
            var _delta = parseInt(e.originalEvent.wheelDelta||-e.originalevent.detail);
            if (_delta > 0) {
          	  init(map.getZoom()+1);
             } else {
          	  init(map.getZoom()-1);
             }
       });
	};
	allmap_on();
     function circle(d,pointA,pointB){
    	      var  statistics=0;
    	      var  s=[];
    	      for(var i=0;i<d.length;i++){
    	    	  if(d[i].point.lng<pointA.lng||d[i].point.lng>pointB.lng){
    	    		  continue;
    	    	  }
    	    	  if(d[i].point.lat>pointA.lat||d[i].point.lat<pointB.lat){
    	    		  continue;
    	    	  }
    	    	  s.push(d[i]);
    	    	  statistics=statistics+(d[i].count*1);
    	      }
     return {list:s,count:statistics};
     };
     //初始化麻点图
     var customLayer;
     var customLayer2;
     //显示麻点图
     function  md(zoom){
    	 if (customLayer||customLayer2) {
  			map.removeTileLayer(customLayer);
  			map.removeTileLayer(customLayer2);
  		 }
    	 if(zoom>10){
    		 customLayer=new BMap.CustomLayer({
    				geotableId:138201,//门店：138201 服务商：138155
    				q: '', //检索关键字
    				tags: '', //空格分隔的多字符串
    				filter: ''
    			});
    		 customLayer2=new BMap.CustomLayer({
	 				geotableId:138155,//门店：138201 服务商：138155
	 				q: '', //检索关键字
	 				tags: '', //空格分隔的多字符串
	 				filter: ''
 			});
        	 map.addTileLayer(customLayer);
        	 map.addTileLayer(customLayer2);
        	// customLayer.addEventListener('hotspotclick',callback);
    	 }
     };
     //请求矩形接口
     //http://api.map.baidu.com/geosearch/v3/bound?ak=A4749739227af1618f7b0d1b588c0e85&geotable_id=30960&bounds=116.30,36.20;117.30,37
     //渲染页面
     function  init(zoom){
    	 md(zoom);
    	 if((zoom*1)<=10){
    		 if(!present){
    			 map.clearOverlays();
    			 var opts=$("#th").drag('opts');
    			 initialize(datas,opts.the_min,opts.the_max);
    			 $("ul.max_ul").css("display","block");
            	 $("ul.min_ul").css("display","none");
            	 button.cancel();
            	 switch (select_type) {   
					case 1:
						$("li.show_gd").click();
						break;
					case 2:
						$("li.show_md").click();
						break;
					case 3:
						$("li.show_fw").click();
						break;
					case 4:
						$("li.show_gc").click();
						break;
				}
            	 present=true;
            	 $("#drag").css('display','block');
        	 }
    	 }else{
    		    map.clearOverlays();
    		    if(present){
		        	 $("ul.max_ul").css("display","none");
		        	 $("ul.min_ul").css("display","block");
		        	 $("#drag").css('display','none');
		        	 button.retrieval_field(2);
		        	 button.cancel();
	        	 }
    		     qj_updateData();
    			 present=false;
    	 }
     };
     //加载区间 函数
     function load_sectionP(){
    	 map.clearOverlays();
    	 for(var i=0;i<qj.store.length;i++){
        		 overlay_complex(qj.store[i].point,qj.store[i].city,qj.store[i].count,2);
    	 }
     };

     //创建圆点 
	function overlay_round(data,city,count){
	    function ComplexCustomOverlay(point, text, count){
	      this._point = point;
	      this._text = text;
	      this._count = count;
	    }
	    ComplexCustomOverlay.prototype = new BMap.Overlay();
	    ComplexCustomOverlay.prototype.initialize = function(map){
	      this._map = map;
	      var self=this;
	      var div = this._div = document.createElement("div");
	      div.style.position = "absolute";
	      div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
	      div.style.backgroundColor = getColor(self._count);
	      div.style.height = "18px";
	      div.style.width = "18px";
	      div.style.borderRadius="10px";
	      div.style.cursor="pointer";  
	      var that = this;
	      div.onmouseover = function(){
	    	  var _count=$("<span class='_count map_show'></span>").append(self._count);
	    	  var _text=$("<span class='_text map_show'></span>").append(self._text);
	    	  var brief=$("<div>").addClass("map_brief").append(_text).append(_count);
	          $(this).append(brief);
	      }
	      div.onmouseout = function(){
               $(this).empty();
	      }
	      map.getPanes().labelPane.appendChild(div);
	      return div;
	    }
	    ComplexCustomOverlay.prototype.draw = function(){
	      var map = this._map;
	          pixel = map.pointToOverlayPixel(this._point);
	      this._div.style.left = pixel.x  +"px";
	      this._div.style.top  = pixel.y - 10 + "px";
	    }
	    var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(data.lng,data.lat),city,count);//(坐标，城市名称， 数量)
	    map.addOverlay(myCompOverlay);
	};
	
	//创建 供应商 和 门店 标记  type (1=门店，2=供应商)
	function  overlay_complex(data,city,count,type){
	    function ComplexCustomOverlay(point, text, count,type){
		      this._point = point;
		      this._text = text;
		      this._count = count;
		      this._type = type;
		    }
		    ComplexCustomOverlay.prototype = new BMap.Overlay();
		    ComplexCustomOverlay.prototype.initialize = function(map){
		      this._map = map;
		      var self=this;
		          div = this._div = document.createElement("div");
              div.style.position = "absolute";
              div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
              div.style.height = "40px";
              div.style.width = "29px";
              div.style.lineHeight = "18px";
              div.style.MozUserSelect = "none";
              div.style.cursor="pointer";
              div.style.fontSize = "12px";
              if(self._type==1){
            	  div.style.background = " url("+url+"image/mark_1.png)no-repeat";
              }else{
            	  div.style.background = " url("+url+"image/mark_2.png)no-repeat";
            	  var pen=document.createElement("div");
            	  pen.className="map_pen"
            	  pen.appendChild(document.createTextNode(self._count));
            	  div.appendChild(pen);
            	  //标志
            	  var lo=document.createElement("div");
            	  lo.className="map_lo"
            	  lo.appendChild(document.createTextNode("H"));
            	  div.appendChild(lo);
              }
              div.style.backgroundSize = "100% 100%";
              
		      var that = this;
		      div.onmouseover = function(){
		    	  
		      }
		      div.onmouseout = function(){
	             //  $(this).empty();
		      }
		      map.getPanes().labelPane.appendChild(div);
		      return div;
		    }
		    ComplexCustomOverlay.prototype.draw = function(){
		      var map = this._map;
		          pixel = map.pointToOverlayPixel(this._point);
		      this._div.style.left = pixel.x -5 +"px";
		      this._div.style.top  = pixel.y + "px";
		    }
		    var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(data.lng,data.lat),city,count,type);//(坐标，城市名称， 数量)
		    map.addOverlay(myCompOverlay);
	}
	//创建固定市悬浮物
	function create_city(data){
		map.centerAndZoom(new BMap.Point(data.lng,data.lat), 11);//以该市为中心的
		for(var i=0;i<data.children.length;i++){
			 overlay_complex(data.children[i].point,data.children[i].city,data.children[i].count);
		}
	};
	function getColor(count){
		count=count*1;
		var basic=(imp.max/200);
		if(count<imp.max){
			return g1[parseInt(count/basic)];
		}else{
			return g1[200];
		}
	};
	//获得可视范围
	function  map_visuals(){
		var left=map.getBounds().getSouthWest().lng+","+map.getBounds().getNorthEast().lat;
		var right=map.getBounds().getNorthEast().lng+","+map.getBounds().getSouthWest().lat;
		return {left:left,right:right};
	};
	//请求区间门店供应商数据
	function  ajax_load(){
		map_visuals();
		return  "";
	};
	//combobox
	$("#sele_city").comboarea({
		 width:160,
		 data:area,
		 path:url+'plugins/comboarea/img',
		 onchange:function(opts){
			 $map.paint('cancel');
			 if(opts.value==0){
				 map.centerAndZoom(new BMap.Point(107.720476,38.019292), 5);//回到全国状态
				 init(map.getZoom());
			 }else{
				 var poi,pic;
				     poi=opts.value.split(",");
				     pic=poi[1].split("|");
				 map.centerAndZoom(new BMap.Point(poi[0],pic[0]), 11);
				 var map_visual=map_visuals();
				 var item=$("#sele_item").combobox("opts");
				 //时间
				 var m_opts=$("#month_qj").combodate('opts');
				     y_opts=$("#year_qj").combodate('opts');
					 date=y_opts.data[y_opts.value].value;
					 if(m_opts.activate){
						 date=date+"-"+m_opts.data[m_opts.value].value;
					 }
				 $.ajax({
						url:urls.store,
						data:"positionleft="+map_visual.left+"&positionright="+map_visual.right+"&project="+item.val_val+"&date="+date,
						async:false,
						success: function(data){
							qj.store=data;
							present=true;
							init(map.getZoom());
							$("li.show_gd_qj").click();
					    }
				});
			 }
		}
	});
	//修改区间数据时调用
	function qj_updateData(){
		 var item=$("#sele_item").combobox("opts");//项目
		     map_visual=map_visuals();//区间坐标
			 m_opts=$("#month_qj").combodate('opts');
		     y_opts=$("#year_qj").combodate('opts');
			 date=y_opts.data[y_opts.value].value;//时间
			 if(m_opts.activate){
				 date=date+"-"+m_opts.data[m_opts.value].value;
			 }
			 $.ajax({
					url:urls.store,
					data:"positionleft="+map_visual.left+"&positionright="+map_visual.right+"&project="+item.val_val+"&date="+date,//
					async:false,
					success: function(data){
						qj.store=data;	
						load_sectionP();
						button.click_gd_qj(qj.store);
				    }
				});
	};
	//修改区间数据时调用
	function qu_updateData(){
		 var item=$("#sele_item").combobox("opts");//项目
		     map_visual=map_visuals();//区间坐标
			 m_opts=$("#month_qu").combodate('opts');
		     y_opts=$("#year_qu").combodate('opts');
			 date=y_opts.data[y_opts.value].value;//时间
			 if(m_opts.activate){
				 date=date+"-"+m_opts.data[m_opts.value].value;
			 }
			 $.ajax({
					url:urls.store,
					data:"positionleft="+map_visual.left+"&positionright="+map_visual.right+"&project="+item.val_val+"&date="+date,//
					async:false,
					success: function(data){
						qj.store=data;	
						load_sectionP();
						var mou=$map.paint("change");
						/*if(mou.direction==2){
							var lng=mou.begin_lng;
						    var lat=mou.begin_lat;
						    mou.begin_lng=mou.over_lng;
						    mou.begin_lat=mou.over_lat;
						    mou.over_lng=lng;
						    mou.over_lat=lat;
						}else  if(mou.direction==3){
						    var lat=mou.begin_lat;
						    mou.begin_lat=mou.over_lat;
						    mou.over_lat=lat;
						}else if(mou.direction==4){
							var lng=mou.begin_lng;
						    mou.begin_lng=mou.over_lng;
						    mou.over_lng=lng;
						} 
						button.Area52(mou);*/
						//button.click_gd_qj(qj.store);
				    }
				});
	};
	$.ajax({
		url:urls.item,
		async:false,
		success: function(data){
			$("#sele_item").combobox({
				width:160,
				checked:true,
				data:data,
				hint:'请选择项目',
				path:url+'plugins/combobox/img',
				onchange:function(opts){
					var date_opts="";
					    m_opts="";
					    date_value="";
					if(map.getZoom()>=10){
						m_opts=$("#month_qj").combodate('opts');
						date_opts=$("#year_qj").combodate('opts');
						if(!m_opts.activate){
							date_value=date_opts.data[date_opts.value].value;
						}else{
							date_value=date_opts.data[date_opts.value].value+"-"+m_opts.data[m_opts.value].value;;
						}
						var map_visual=map_visuals();
						$.ajax({
							url:urls.store,
							data:"positionleft="+map_visual.left+"&positionright="+map_visual.right+"&project="+opts.val_val+"&date="+date_value,//
							async:false,
							success: function(data){
								qj.store=data;	
								present=false;
								load_sectionP();
								button.click_gd_qj(qj.store);
						    }
						});
					}else{
						m_opts=$("#month_t").combodate('opts');
						date_opts=$("#year_t").combodate('opts');
						if(m_opts!=""&&date_opts!=""){
							if(!m_opts.activate){
								date_value=date_opts.data[date_opts.value].value;
							}else{
								date_value=date_opts.data[date_opts.value].value+"-"+m_opts.data[m_opts.value].value;;
							}
						}
						$.ajax({
							url:urls.sum_work,
							data:"statisticstype=0&project="+opts.val_val+"&date="+date_value,
							async:false,
							success: function(data){
								 datas.sum_work=data[0];
								 var opts=$("#th").drag('opts');
				    			 initialize(datas,opts.the_min,opts.the_max);
				    			 button.click_gd(datas.sum_work);
						    }
						});
					}
					
				}
			});
	    }
	});
	//排序集合(data=需要排序集合，排序类型[正序，或者倒序]，field=排序字段)
	function sort_list(data,type,field){
		if(type===1){
			 for(var i=0;i<data.length-1;i++){
				  for(var j=0;j<data.length-i-1;j++){
				   if(data[j][field]<data[j+1][field]){
					    var swap=data[j];
					    data[j]=data[j+1];
					    data[j+1]=swap;
				   }
				  }
			}
		}else if(type===2){
			 for(var i=0;i<data.length-1;i++){
				  for(var j=0;j<data.length-i-1;j++){
				   if(data[j][field]>data[j+1][field]){
					    var swap=data[j];
					    data[j]=data[j+1];
					    data[j+1]=swap;
				   }
				  }
			}
		}
		return data;
	};
	//初始化隐藏
	
	//悬浮物部分
	var menu={
	     one:function(){},
	     tow:function(){}
	};
	//初始化面板 type=1,2,3,4 分别对应四种类型数据  区间  5 ，6
	function setPanels(type){
		 if(type<5){
			 select_type=type;
			 var opts=$("#th").drag('opts');
			 initialize(datas,opts.the_min,opts.the_max);
		 } 
		 //初始化panels
    	switch (type) {
 		case 1://单量
 			button.retrieval_field(2);
 			button.click_gd(datas.sum_work);
 			break;
 		case 2://门店
 			button.retrieval_field(1);
 			button.click_md(datas.sum_store);
 			break;
 		case 3://服务商
 			button.retrieval_field(1);
 			button.click_fw(datas.sum_service);
 			break;
 		case 4://工程师
 			button.retrieval_field(1);
 			button.click_gc(datas.sum_engineer);
 			break;
 		case 5://单量 （区间）
 			button.click_gd_qj(qj.store);
 			break;
 		case 6://区域划分（区间）
 			
 			break;
 		}
	};
	//按钮重绘
	$('#but_ch').click(function(){
		$("#hf_centre").empty();
		$map.paint("open"); 
	});
	//按钮   （区间-----工单）
	$("li.show_gd_qj").on('click',function(){
		if($(this).hasClass("open")){
			 //$(this).removeClass("open");
			 //$("div.panels").css("display","none");
		}else{
			 $("div.panels").css("display","none");
			 $(this).siblings().removeClass("open");
			 $(this).addClass("open");
			 setPanels(5);//单量
			 $("#work_section").css("display","block");
		}
	});
	//按钮   （区间-----划分）
	$("li.show_gd_qy").on('click',function(){
		if($(this).hasClass("open")){
			 //$(this).removeClass("open");
			 //$("div.panels").css("display","none");
		}else{
			$(this).siblings().removeClass("open");
			$(this).addClass("open");
			$("div.panels").css("display","none");
			$("#show_hf").css("display","block");
			button.click_gd_qu(qj.service);
		}
	});
	
	//按钮   （全球-----工单）
	$("li.show_gd_but").on('click',function(){
		$(this).siblings().removeClass("open");
		if($(this).hasClass("open")){
			 $(this).removeClass("open");
			 $("div.panels").css("display","none");
			 button.retrieval_field(1);
		}else{
			$(this).addClass("open");
			$("div.panels").css("display","none");
			update_time();
			$("#work").css("display","block");
		}
	});
	//按钮   （全球-----门店）
	$("li.show_md").on('click',function(){
		$(this).siblings().removeClass("open");
		if($(this).hasClass("open")){
			 $(this).removeClass("open");
			 $("div.panels").css("display","none");
		}else{
			 $(this).addClass("open");
			 $("div.panels").css("display","none");
			 setPanels(2);//门店
			 $("#common").css("display","block");
		}
	});
	//按钮   （全球-----服务商）
	$("li.show_fw").on('click',function(){ 
		$(this).siblings().removeClass("open");
		if($(this).hasClass("open")){
			 $(this).removeClass("open");
			 $("div.panels").css("display","none");
		}else{
			 $(this).addClass("open");
			 $("div.panels").css("display","none");
			 setPanels(3);//服务商
			 $("#common").css("display","block");
		}
	});
	//按钮   （全球-----工程师）
	$("li.show_gc").on('click',function(){
		$(this).siblings().removeClass("open");
		var self=$(this);
		if($(this).hasClass("open")){
			 $(this).removeClass("open");
			 $("div.panels").css("display","none");
		}else{
			$.ajax({
				url:urls.sum_engineer,
				data:"statisticstype=3&project=&date=",
				async:false,
				success: function(data){
					datas.sum_engineer=data[0];
					self.siblings().removeClass("open");
					if(self.hasClass("open")){
						 self.removeClass("open");
						 $("div.panels").css("display","none");
					}else{
						 self.addClass("open");
						 $("div.panels").css("display","none");
						 setPanels(4);//工程师
						 $("#common").css("display","block");
					}
			    }
			});
		}
	});
	//全国工单控件渲染
	$("#year_t").combodate({
    	width:130,
    	value:2,
    	path:url+'plugins/combodate/img',
    	onchange:function(opts){
    		update_time();
    	}
    });
	//区域工单
	$("#year_qu").combodate({
    	width:130,
    	value:2,
    	path:url+'plugins/combodate/img',
    	onchange:function(opts){
    		qu_updateData();
    	}
    });
	$("#month_qu").combodate({
    	width:130,
    	data:monthss,
    	activate:false,
    	path:url+'plugins/combodate/img',
    });
	$('#qu_check').click(function(){
		if($(this).attr('checked')=='checked'){
			$("#month_qu").combodate({
		    	width:130,
		    	data:monthss,
		    	activate:false,
		    	path:url+'plugins/combodate/img',
		    });
		}else{
			$("#month_qu").combodate({
		    	width:130,
		    	data:monthss,
		    	activate:true,
		    	path:url+'plugins/combodate/img',
		    	onchange:function(opts){
		    		qu_updateData();
		    	}
		    });
		}
		$("#year_qu").combodate("chenge");
	});
	//区间工单
	$("#year_qj").combodate({
    	width:130,
    	value:2,
    	path:url+'plugins/combodate/img',
    	onchange:function(opts){
    		qj_updateData();
    	}
    });
	$("#month_qj").combodate({
    	width:130,
    	data:monthss,
    	activate:false,
    	path:url+'plugins/combodate/img',
    });
	$('#qj_check').click(function(){
		if($(this).attr('checked')=='checked'){
			$("#month_qj").combodate({
		    	width:130,
		    	data:monthss,
		    	activate:false,
		    	path:url+'plugins/combodate/img',
		    });
		}else{
			$("#month_qj").combodate({
		    	width:130,
		    	data:monthss,
		    	activate:true,
		    	path:url+'plugins/combodate/img',
		    	onchange:function(opts){
		    		qj_updateData()
		    	}
		    });
			
		}
		$("#year_qj").combodate("chenge");
	});
	
	//全国工单
	$("#month_t").combodate({
    	width:130,
    	data:monthss,
    	activate:false,
    	path:url+'plugins/combodate/img',
    });
	$('#gd_check').click(function(){
		if($(this).attr('checked')=='checked'){
			$("#month_t").combodate({
		    	width:130,
		    	data:monthss,
		    	activate:false,
		    	path:url+'plugins/combodate/img',
		    });
			update_time();
		}else{
			$("#month_t").combodate({
		    	width:130,
		    	data:monthss,
		    	activate:true,
		    	path:url+'plugins/combodate/img',
		    	onchange:function(opts){
		    		update_time();
		    	}
		    });
			update_time();
		}
	});
	function update_time(){
		var d=$("#sele_item").combobox('opts');
		var opts=$("#month_t").combodate('opts');
		var y_opts=$("#year_t").combodate('opts');
		var date_value="";
		if(opts.activate){
			 date_value=y_opts.data[y_opts.value].value+"-"+opts.data[opts.value].value;
		}else{
			 date_value=y_opts.data[y_opts.value].value
		}
		$.ajax({
			url:urls.sum_work,
			data:"statisticstype=0&project="+d.val_val+"&date="+date_value,
			async:false,
			success: function(data){
				datas.sum_work=data[0];
				/*var op=$("#th").drag('opts');
				initialize(datas,op.the_min,op.the_max);*/
				setPanels(1);
		    }
		});
	};

    //按钮加载
	var button={
			cancel:function(){
				$("ul.dg_ul").find("li.open").removeClass("open");
				$("div.panels").css("display","none");
			},
			click_gd:function(data){//全国-单量
				var c=$("#panel_gf_menu").empty();
				var title=$("<div class='gd_title'> <div class='gd_left'>工单统计</div> <div class='gd_right'>"+data.sun+"单</div></div>");
				c.append(title);
				for(var i=0;i<data.list.length;i++){
					var left=$("<div class='gd_left'>"+data.list[i].city+"</div>");
					    right=$("<div class='gd_right'>"+data.list[i].count+"</div>");
					    d=$("<div class='gd_content'>").append(left).append(right);
					c.append(d);
				}
			},
			click_md:function(data,value){//门店
				var t1=$("<div class='md_select'>").append("城市门店数量排行");
				var select=$("<div class='md_selects'>").append("<div id='sele' style='margin-top:3px;'>");
				var s=$("<div class='md_menu'>");
				for(var i=0;i<data.list.length;i++){
					var left=$("<div class='md_left'>").append(data.list[i].city);
					    right=$("<div class='md_right'>").append(data.list[i].count);
					    d=$("<div class='md_content'>").append(left).append(right);
					s.append(d);
				}
				var title=$("<div class='md_title'></div>").append(t1).append(select);
				$("#common").empty().append(title).append(s);
				$("#sele").combobox({
					width:200,
			    	data:ps,
			    	hint:'排序',
			    	value:value,
			    	path:url+'plugins/combobox/img',
			    	onchange:function(opts){
			    		 var d=sort_list(datas.sum_store.list,(opts.data[opts.value].value*1),'count');
			    		 datas.sum_store.list=d;//重新赋值
			    		 button.click_md(datas.sum_store,opts.value);//重新渲染列表
			    	}
				});
			},
			click_fw:function(data,value){//服务商
				var t1=$("<div class='md_select'>").append("城市服务商数量排行");
				var select=$("<div class='md_selects'>").append("<div id='sele_fw' style='margin-top:3px;'>");
				var s=$("<div class='md_menu'>");
				for(var i=0;i<data.list.length;i++){
					var left=$("<div class='md_left'>").append(data.list[i].city);
					    right=$("<div class='md_right'>").append(data.list[i].count);
					    d=$("<div class='md_content'>").append(left).append(right);
					s.append(d);
				}
				var title=$("<div class='md_title'></div>").append(t1).append(select);
				$("#common").empty().append(title).append(s);
				$("#sele_fw").combobox({
					width:200,
			    	data:ps,
			    	hint:'排序',
			    	value:value,
			    	path:url+'plugins/combobox/img',
			    	onchange:function(opts){
			    		 var d=sort_list(datas.sum_service.list,(opts.data[opts.value].value*1),'count');
			    		 datas.sum_service.list=d;//重新赋值
			    		 button.click_fw(datas.sum_service,opts.value);//重新渲染列表
			    	}
				});
			},
			click_gc:function(data,value){//工程师
				var t1=$("<div class='md_select'>").append("城市工程师数量排行");
				    select=$("<div class='md_selects'>").append("<div id='sele_gc' style='margin-top:3px;'>");
				    s=$("<div class='md_menu'>");
				for(var i=0;i<data.list.length;i++){
					//var icon=(data.list[i].rise*1)>0 ? "../../WEB-INF/map/img/xiajiao_red.svg":"../../WEB-INF/map/img/xiajiao.svg"
					var left=$("<div class='md_left'>").append(data.list[i].city);
					    right=$("<div class='md_right'>").append(data.list[i].count);
					    d=$("<div class='md_content'>").append(left).append(right);
					s.append(d);
				}
				var title=$("<div class='md_title'></div>").append(t1).append(select);
				$("#common").empty().append(title).append(s);
				$("#sele_gc").combobox({
					width:200,
			    	data:ps,
			    	hint:'排序',
			    	value:value,
			    	path:url+'plugins/combobox/img',
			    	onchange:function(opts){
			    		 var d=sort_list(datas.sum_engineer.list,(opts.data[opts.value].value*1),'count');
			    		 datas.sum_service.list=d;//重新赋值
			    		 button.click_fw(datas.sum_engineer,opts.value);//重新渲染列表
			    	}
				});
			},
			click_gd_qj:function(data){//单量
				var c=$("<div class='gd_menu'>");
				    m_opts=$("#month_qj").combodate('opts');
			        y_opts=$("#year_qj").combodate('opts');
			        date=y_opts.data[y_opts.value].text;
			        if(m_opts.activate){
			        	date=date+m_opts.data[m_opts.value].text;
			        }
				    dl=$("<div class='gd_left'>工单统计</div> <div class='gd_right'>"+date+"</div>");
				    title=$("<div class='gd_title'>").append(dl);
				    c.append(title);
				for(var i=0;i<data.length;i++){
					var left=$("<div class='gd_left'>"+data[i].name+"</div>");
					    right=$("<div class='gd_right'>"+data[i].count+"</div>");
					    d=$("<div class='gd_content'>").append(left).append(right);
					c.append(d);
				}
				$("#panel_gf_menu_section").empty().append(c);
			},
			click_gd_qu:function(data){//区域划分
			/*	$("#show_hf").css("display","block");
			    var year_t=$("<div id='year_qu' style='float:left;'>").css({
			    	'margin-left': '8px',
			    	'margin-top': '7px'
			    });
			    checl_label=$("<span class='gd_checl_label'>").append("全年");
			    check=$("<input id='qu_check' class='gd_check' type='checkbox' checked='checked'>");
			    check_vessel=$("<div class='gd_check_vessel'>").append(checl_label).append(check);
		        year=$("<div class='gd_opts'>").append(year_t).append(check_vessel);
		        month_t=$("<div id='month_qu' style='float:left;'>").css({
		        	'margin-left': '8px',
			    	'margin-top': '7px'
			    });
		        month=$("<div class='gd_opts'>").append(month_t).css('border-top','1px solid #ccc');
		        $("#show_hf").find('div.hf_select').append(year).append(month);*/

				$map.paint("open"); 
			},
			Area52:function(mous){
            	var pointA = new BMap.Point(mous.begin_lng,mous.begin_lat); 
	        	    pointB = new BMap.Point(mous.begin_lng,mous.over_lat); 
	        	    pointC = new BMap.Point(mous.over_lng,mous.over_lat); 
	        	    long=(map.getDistance(pointA,pointB).toFixed(2))/1000;
	        	    tall=(map.getDistance(pointB,pointC).toFixed(2))/1000;
        	        mj=((long*tall)+"").split('.');
        	        opts_y=$("#year_qu").combodate('opts');
        	        opts_m=$("#month_qu").combodate('opts');
        	        sj="<span>"+opts_y.data[opts_y.value].value+"</span>年";
        	        if(opts_m.activate===true){
        	        	sj+="<span>"+opts_m.data[opts_y.value].value+"</span>月"
        	        }
        	        var kl=$("#year_qu").data("combodate");
				var self=$("#hf_centre").empty();
				    date=$("<div class='hf_date hf_detail'>").append("<div class='detail_left'>时间</div><div class='detail_right'>"+sj+"</div>");
	                area=$("<div class='hf_area hf_detail'>").append("<div class='detail_left'>区域面积</div><div class='detail_right'><span>"+mj[0]+"</span> km²</div>");
	                d= circle(qj.store,pointA,pointC);
	                count=$("<div class='hf_count hf_detail'>").append("<div class='detail_left'>工单总量</div><div class='detail_right'><span>"+d.count+"</span> 单</div>");
	                mess=$("<div class='hf_mess'>").append("<div class='detail_left'>门店信息</div><div class='detail_right'><span>"+d.list.length+"</span> 家</div>");
                    for(var i=0;i<d.list.length;i++){
                      var icon=$("<div class='meess_icon'>").append("<img src="+url+"image/bj.svg>");
                          up=$("<div class='mess_right_up'>").append("<div class='up_name'>"+d.list[i].name+"</div><div class='up_count'><span>"+d.list[i].count+"</span> 单</div>");
                          down=$("<div class='mess_right_down'>").append("");
                          right=$("<div class='mess_right'>").append(up).append(down);
                          div=$("<div class='mess_z'>").append(icon).append(right);
                      mess.append(div);
                    }
	             self.append(date).append(area).append(count).append(mess);   
			},
			retrieval_field:function(type){
				if(type==1){
					$("#map_select").css("width",'170px');
					$("#sele_item").css("display",'none');
				}else if(type==2){
					$("#map_select").css("width",'320px');
					$("#sele_item").css("display",'block');
				}
			},
	};
	//初始化隐藏项目选择
	button.retrieval_field(1);
	   function ajax(url,param){
		   var d;
			$.ajax({
				url:url,
				data:param,
				async:false,
				success: function(data){
					d=data
					return d;
			    }
			});
		};
    // 私有函数：调试输出
    function debug(info) {
        if (window.console) {
            window.console.log("debug[autoform] | " + JSON.stringify(info));
        }
    }
	
});